#import "SFUWebSource.h"

/**
 * An SFUWebSource is an object representing an SFU website 
 * with parsable events.
 *
 * Conforms to the WebParsable protocol.
 *
 * @author      James Lin
 * @version     0.5.0
 * @since       0.0.2
 * @see         WebParsable
 */
@implementation SFUWebSource

@synthesize sourceURL;
@synthesize sourceCode;

/**
 * Initialize the SFU website load request with a URL.
 *
 * The connection is asynchronous.
 *
 * @param sourceURLString  the absolute URL of the SFU website
 * @param theDelegate      the delegate to handle connection events
 * @return                 the initialized SFUWebSource
 * @see	                   WebParsable#initWithURLString
 * @see	                   SFUConnectionDelegate
 */
- (id) initWithURLString:(NSString*)sourceURLString
				delegate:(id<SFUConnectionDelegate>)theDelegate {
	if (self = [super init]) {
		self.sourceURL = [NSURL URLWithString:sourceURLString];
		delegate = theDelegate;
	}
	
	return self;
}

/**
 * Get the delegate handling connection events.
 *
 * @return  the delegate handling connection events
 */
- (id)delegate {
	return delegate;
}

/**
 * Initialize the load request to SFU website and retrieve source code.
 */
- (void) initConnection {
	request = [NSMutableURLRequest requestWithURL:self.sourceURL 
									  cachePolicy:NSURLRequestUseProtocolCachePolicy 
								  timeoutInterval:CONN_TIMEOUT_INTERVAL];
	connection = [[NSURLConnection alloc] initWithRequest:request 
												 delegate:self];
	
	if (connection) {
		receivedData = [[NSMutableData data] retain];
	} else {
		NSLog(@"Connection error: %@", connection);
	}
}

/**
 * Parse the events from the website.
 *
 * MUST override this method if parsing events is needed.
 *
 * @return  an array of the parsed events
 * @see	    WebParsable#parseEvents
 */
- (NSArray*) parseEvents {
	return nil;
}

/**
 * Handle NSURLConnection response received event.
 */
- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response {
	[receivedData setLength:0];
	if ([delegate respondsToSelector:@selector(connection:didRespond:)]) {
		[delegate connection:theConnection didRespond:response];
	}
}

/**
 * Handle NSURLConnection data received event.
 */
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data {
	[receivedData appendData:data];
	if ([delegate respondsToSelector:@selector(connection:didReceiveData:)]) {
		[delegate connection:theConnection didReceiveData:data];
	}
}

/**
 * Handle NSURLConnection failed with error event.
 */
- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error {
    [self.delegate connectionDidFail:theConnection];
}

/**
 * Handle NSURLConnection finished loading event.
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
	self.sourceCode = [[NSString alloc] initWithData:receivedData 
											encoding:NSASCIIStringEncoding];
	[self.delegate connectionDidFinish:theConnection 
							   results:nil];
}

/**
 * Deallocate the SFUWebSource.
 */
- (void)dealloc {
	[sourceURL release];
	[sourceCode release];
	[receivedData release];
	[connection release];
	[super dealloc];
}

@end
